package com.financing.platform.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.AccessInfoDTO;
import com.financing.platform.dto.ActionDTO;
import com.financing.platform.dto.GroupDTO;
import com.financing.platform.service.IAccessControlService;
import com.financing.platform.service.IActionService;
import com.financing.platform.service.IGroupService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;

/**
 * 
* @ClassName: SearchGroup 
* @Description: 组管理 
* @author chenming 
* @date 2014年7月21日 上午11:45:33 
*
 */
@Controller
public class GroupAction extends BaseAction{
	
	@Autowired
    IGroupService groupServiceImpl;
	
	@Autowired
	IAccessControlService accessControlServiceImpl;
	
	@Autowired
	IActionService actionServiceImpl;
	
	@RequestMapping("/queryGroupPer.shtml")
	public ModelAndView queryGroupPer(HttpServletRequest request,
			HttpServletResponse response ) {
		
		List<ActionDTO> actionList = actionServiceImpl.queryAllAction();
		Map<String, Object> actionMap = new HashMap<String,Object>();
		actionMap.put("actionList", actionList);
		
		request.getSession().setAttribute(Dict.requestID, "queryGroupPer.shtml");
		
		return new ModelAndView("groupPer",actionMap);
	}
	
	/**
	 * 
	* @Title: searchAllGroup 
	* @Description: 查询所有组信息概要
	* @param   request
	* @param   response
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/queryGroup.shtml")
	public ModelAndView searchAllGroup(HttpServletRequest request,
			HttpServletResponse response,GroupDTO group) {
		this.log.info("begin query all Group!");
		
		Map<String,List<GroupDTO>> groupdDTOMap  = new HashMap<String,List<GroupDTO>>();
		
		groupdDTOMap.put("allGroup",groupServiceImpl.queryAllGroup(group));
		
		this.log.info("end query all Group !");
		
		return new ModelAndView("groupList", groupdDTOMap);
	}
	
	/**
	 * 
	* @Title: searchAllGroup 
	* @Description: 查询所有组信息概要
	* @param   request
	* @param   response
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/queryGroupInfo.shtml")
	public ModelAndView queryGroupInfo(HttpServletRequest request,
			HttpServletResponse response,GroupDTO group) {
		this.log.info("begin query one Group!");
		
		Map<String,Object> groupDTOMap  = new HashMap<String,Object>();
		
		groupDTOMap.put("groupInfo",groupServiceImpl.queryGroup(group));
		
		List<AccessInfoDTO> accessForGroupList = accessControlServiceImpl.queryAccessForGroup(group);
		
		groupDTOMap.put("accessForGroup", accessForGroupList);
		
		List<ActionDTO> actionDtoList = actionServiceImpl.queryAllAction();
		
		groupDTOMap.put("allAction", actionDtoList);
		
		this.log.info("end query one Group !");
		
		return new ModelAndView("groupModify", groupDTOMap);
	}
	
	/**
	 * 
	* @Title: insertGroup 
	* @Description: 添加组
	* @param   request
	* @param   response
	* @param   groupDto
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/addGroup.shtml")
	public ModelAndView insertGroup(HttpServletRequest request,
			HttpServletResponse response,GroupDTO groupDto) {
		
		this.log.info("begin add  Group!");

		Map<String,Integer> mode = new HashMap<String,Integer>();
		
		if(!StringUtil.isNullOrEmptyStr(groupDto.getGroupName()) && !StringUtil.isNullOrEmptyStr(groupDto.getGroupRemark())){
			DTOdateSet.setCreateInfo(groupDto, request.getSession());
			DTOdateSet.setUpdateInfo(groupDto, request.getSession());
			
			mode.put("result", groupServiceImpl.addGroup(groupDto));
			
			accessControlServiceImpl.deleteAccessForGroup(groupDto);
			
			String[] actionIds = request.getParameterValues("actionId");
			
			AccessInfoDTO accessInfoDTO = new AccessInfoDTO();
			DTOdateSet.setCreateInfo(accessInfoDTO, request.getSession());
			DTOdateSet.setUpdateInfo(accessInfoDTO, request.getSession());
			accessInfoDTO.setGroupId(groupDto.getGroupId());
			accessInfoDTO.setEnable(Dict.accessEnable);
			accessControlServiceImpl.addAccessForGroup(actionIds,accessInfoDTO);
		}else{
			mode.put("result", 0);
		}
		
		
		this.log.info("end add Group !");
		
		return new ModelAndView("groupModifyResult", mode);
	}
	
	/**
	 * 
	* @Title: queryGroupAccess 
	* @Description: 更新组权限信息
	* @param   request
	* @param   response
	* @param   groupDto
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/updateGroupInfo.shtml")
	public ModelAndView updateGroupInfo(HttpServletRequest request,
			HttpServletResponse response,GroupDTO groupDto) {
		
		this.log.info("begin update group info!");
		
		groupServiceImpl.updateGroup(groupDto);
		
		this.log.info("end update group info!");
		
		String[] actionIds = request.getParameterValues("actionId");
		
		this.log.info("begin update group access !");
		AccessInfoDTO accessInfoDTO = new AccessInfoDTO();
		DTOdateSet.setCreateInfo(accessInfoDTO, request.getSession());
		DTOdateSet.setUpdateInfo(accessInfoDTO, request.getSession());
		accessInfoDTO.setGroupId(groupDto.getGroupId());
		
		accessControlServiceImpl.deleteAccessForGroup(groupDto);
		accessInfoDTO.setEnable(Dict.accessEnable);
		accessControlServiceImpl.addAccessForGroup(actionIds,accessInfoDTO);
		
		Map<String,Object> resultMode = new HashMap<String,Object>();
		resultMode.put("result", 1);
		
		this.log.info("end update group access !");
		
		return new ModelAndView("groupModifyResult", resultMode);
	}
	
}
